package com.eii.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.eii.common.constant.BusinessTypeConstants;
import com.eii.common.constant.UserConstants;
import com.eii.common.web.controller.BaseController;
import com.eii.common.web.domain.Result;
import com.eii.common.web.page.PageResult;
import com.eii.excel.util.ExcelUtils;
import com.eii.log.annotation.AuditLog;
import com.eii.oauth2.common.annotation.RequiresPermissions;
import com.eii.oauth2.common.util.SecurityUtils;
import com.eii.system.controller.vo.SysPostExcel;
import com.eii.system.domain.SysPost;
import com.eii.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 岗位信息操作处理
 */
@RestController
@RequestMapping("/post")
public class SysPostController extends BaseController {

    private final static String CONTROLLER_TITLE = "岗位管理";

    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @RequiresPermissions("system:post:list")
    @GetMapping("/list")
    public PageResult list(SysPost post) {
        startPage();
        List<SysPost> list = postService.selectPostList(post);
        return getPageResult(list);
    }

    @AuditLog(title = CONTROLLER_TITLE, operation = BusinessTypeConstants.EXPORT)
    @RequiresPermissions("system:post:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPost post) throws IOException {
        List<SysPost> list = postService.selectPostList(post);
        List<SysPostExcel> sysPostExcels = BeanUtil.copyToList(list, SysPostExcel.class);
        String datestr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String filename = "岗位数据" + datestr + ".xlsx";
        ExcelUtils.exportExcelStream(response, filename, SysPostExcel.class, sysPostExcels);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @RequiresPermissions("system:post:query")
    @GetMapping(value = "/{postId}")
    public Result getInfo(@PathVariable Long postId) {
        return Result.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @RequiresPermissions("system:post:add")
    @AuditLog(title = CONTROLLER_TITLE, operation = BusinessTypeConstants.INSERT)
    @PostMapping
    public Result add(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return Result.fail("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return Result.fail("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(SecurityUtils.getUsername());
        return toResult(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @RequiresPermissions("system:post:edit")
    @AuditLog(title = CONTROLLER_TITLE, operation = BusinessTypeConstants.UPDATE)
    @PutMapping
    public Result edit(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return Result.fail("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return Result.fail("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(SecurityUtils.getUsername());
        return toResult(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @RequiresPermissions("system:post:remove")
    @AuditLog(title = CONTROLLER_TITLE, operation = BusinessTypeConstants.DELETE)
    @DeleteMapping("/{postIds}")
    public Result remove(@PathVariable Long[] postIds) {
        return toResult(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public Result optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return Result.success(posts);
    }
}
